angular.module('mvcApp.login', [
    'ngRoute',
    'ngResource'
]).config(['$routeProvider', function($routeProvider) {
    $routeProvider
        .when('/login', {
            templateUrl: 'static/views/login.html',
            controller: 'LoginCtrl'
        });
}]).controller('LoginCtrl', ['$scope', '$http', '$location', 'User',
    function ($scope, $http, $location, User) {
        $scope.login = function (username, password, captcha) {
            if (!username || !password || !captcha) {
                alert("用户名密码验证码不可为空");
                return;
            }
            User.login(username, password, captcha)
                .then(function(userInfo) {
                    alert("登陆成功");
                    $scope.username = '';
                    $scope.password = '';
                    window.location.href = '#/home';
                }).catch(function(err) {
                    alert(err);
                });
        };
        $scope.refreshCaptcha = function () {
            $http.get('/wechatshop/captcha/request_for_current_user')
                .success(function (data) {
                    $scope.captchaUrl = '/wechatshop/captcha/view/' + data.data;
                    $scope.$applyAsync();
                }).catch(function (err) {
                    console.log(err);
                })
        };
        $scope.refreshCaptcha();
    }]);